/**
 *  Copyright 2009-2014 by WWW.KNET.CN  All rights reserved.
 */
package com.domain.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.domain.model.CloudWebDish;
import com.domain.model.Dict;
import com.domain.model.Page;
import com.domain.model.Sysuser;
import com.domain.service.CloudWebDishService;
import com.domain.service.DictService;
import com.domain.util.DateUtil;
import com.domain.util.commonUtil;

/**
 * TODO各店风采
 * 
 * @author liujie
 * @date 2015年5月18日 下午3:34:23
 * 
 */
@Controller
@RequestMapping("/storeShow")
public class StoreShowController extends SuperController {
	private static final Logger logger = LoggerFactory.getLogger(StoreShowController.class);
	@Resource
	private CloudWebDishService cloudWebDishService;
	@Resource
	private DictService dictService;
	@RequestMapping("/list.html")
	public String getdishList(Page page, String type, String porCode, ModelMap model,
			HttpServletRequest request) {
		String returnPage = "store/%sList";
		page = null == page ? new Page() : page;
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			CloudWebDish webDish = new CloudWebDish();
			webDish.setPorCode(porCode);
			webDish.setIfDelete(0l);
			paramMap.put(webDish.key, webDish);
			saveReqInfo( model, porCode, type);
			paramMap.put(PAGE_NAME, page.getPage());
			paramMap.put(PAGESIZE_NAME, page.getPageSize());
			paramMap.put("orderBy", "dish_status desc,dish_when desc");
			List<CloudWebDish> messageList = cloudWebDishService.getListPage(paramMap);
			Long total = cloudWebDishService.getRows(paramMap);
			Page page2 = new Page(page.getPage(), page.getPageSize(), Integer.parseInt(total + ""));
			model.addAttribute("dishList", messageList);
			model.addAttribute("page", page2);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return "exception";
		}
		return String.format(returnPage, type);
	}

	// 编辑
	@RequestMapping(value = "/edit/{type}.html")
	public String edit(@PathVariable String type, ModelMap model, String porCode, CloudWebDish cloudWebdish,
			HttpServletRequest request) {
		saveReqInfo( model, porCode, type);
		if (null == cloudWebdish || null == cloudWebdish.getId()) {// add
			cloudWebdish = new CloudWebDish();
			cloudWebdish.setPorCode(porCode);
			model.put("entity", cloudWebdish);
		} else { 
			CloudWebDish msg = cloudWebDishService.get(cloudWebdish.getId());
			model.put("entity", msg);
		}
		return String.format("store/%sEdit",type);
	}
	@RequestMapping(value="/save.html",method=RequestMethod.POST)
	//@ResponseBody
	public String save(String type,ModelMap model,CloudWebDish cloudWebDish, HttpServletRequest request) {
		Sysuser user = getCurrentUser(request);
		MultipartHttpServletRequest mureq = (MultipartHttpServletRequest) request;
		MultipartFile file=mureq.getFile("file");
		try {
			Dict dict=dictService.getDictByCode("filePath");
			String prefixPath =(null !=dict && null !=dict.getDict_value() && !"".equals(dict.getDict_value()))?dict.getDict_value(): request.getServletContext().getRealPath("/");
			String savePath= commonUtil.getFilePath(prefixPath,file);//创建并返回路径;
			
			if (null == cloudWebDish.getId()) {
				cloudWebDish.setDishBy(user.getId());
				cloudWebDish.setClick(0l);
				cloudWebDish.setDishStatus(0l);
				cloudWebDish.setDishAccessory(savePath);
				cloudWebDishService.add(cloudWebDish);
			} else {
					if (StringUtils.isNotBlank(savePath)) {
						cloudWebDish.setDishAccessory(savePath);
					}
				cloudWebDishService.update(cloudWebDish);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("-------------保存带附件的信息异常：" + e.getMessage());
		}
		model.addAttribute(POR_CODE, cloudWebDish.getPorCode());
		model.addAttribute("type", type);
		return "redirect:/storeShow/list.html";
	}	

	@RequestMapping(value = "/delete/{type}.html", method = { RequestMethod.POST, RequestMethod.GET })
	public String delete(@PathVariable String type,ModelMap model, CloudWebDish cloudWebDish, HttpServletRequest request) {
		Sysuser user = getCurrentUser(request);
		try {
			cloudWebDishService.deleteById(cloudWebDish.getId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("-------------删除webdish:" + cloudWebDish.getId() + ";异常：" + e.getMessage());
		}
		saveReqInfo( model, cloudWebDish.getPorCode(), type);
		return "redirect:/storeShow/list.html";
	}
	  //置顶
		@RequestMapping(value = "/head.html")
		public String head(String type, ModelMap model, String porCode, CloudWebDish cloudWebDish,
				HttpServletRequest request) {
			try {
				
			} catch (Exception e) {
				logger.error("--------------------------"+type+"置顶错误：{}"+e.getMessage());
			}
			cloudWebDishService.head(cloudWebDish.getId());
			saveReqInfo( model, porCode,type);
			return "redirect:/+l 4ist.html";
		}
	private void saveReqInfo(ModelMap model, String porCode, String type) {
		model.addAttribute(POR_CODE, porCode);
		model.addAttribute("type", type);
	}
}
